var express = require('express');
var router = express.Router();

var { usersModel, roleModel, IOAModel,roomtypeModel } = require("../../model/model")

/* GET home page. */
router.get('/users_list', async (req, res) => {
  let users_list = await usersModel.find();
  res.send({
    users_list
  })
});

router.get('/role_list', async (req, res) => {
  let role_list = await roleModel.find();
  res.send({
    role_list
  })
});

router.get('/IOA_list', async (req, res) => {
  let IOAlist = await IOAModel.find().lean()
  let cateObj = {}
  IOAlist.forEach(item => {
    cateObj[item._id] = item
  })

  let data = []
  IOAlist.forEach((item) => {
    if (!item["Iid"]) {
      data.push(item)
    } else {
      if (cateObj[item["Iid"]] && !cateObj[item["Iid"]]["children"]) {
        cateObj[item["Iid"]]["children"] = []

      }
      if (cateObj[item["Iid"]]) {
        cateObj[item["Iid"]]["children"].push(item)
      }
    }
  })
  res.send({
    code: 200,
    data
  })
})

router.get('/roomtype',async (req,res)=>{
        let data = await roomtypeModel.find()
        res.send({
          code:200,
          data
        })
} )
router.get('/roomtypes',async (req,res)=>{
  let id = req.query.id
  let data = await roomtypeModel.find({_id:id})
  res.send({
    code:200,
    data
  })
})

module.exports = router;
